package cn.wsalix.admin.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.anlaser.service.impl.RelatedServiceImpl;
import cn.anlaser.utils.SearchFilter;
import cn.anlaser.utils.SearchFilter.Operator;
import cn.wsalix.admin.entity.MenuPermit;
import cn.wsalix.admin.entity.Permit;
import cn.wsalix.admin.entity.SysMenu;
import cn.wsalix.admin.form.MenuPermitForm;
import cn.wsalix.admin.repository.MenuPermitRepository;
import cn.wsalix.admin.service.MenuPermitService;
import cn.wsalix.admin.service.MenuService;
import cn.wsalix.admin.service.PermitService;
import cn.wsalix.exception.ExistException;

@Service("menuPermitService")
public class MenuPermitServiceImpl
		extends
		RelatedServiceImpl<MenuPermitForm, MenuPermitRepository, MenuPermit, SysMenu, Permit>
// CommonServiceImpl<MenuPermitForm, MenuPermitRepository, MenuPermit>
		implements MenuPermitService {
	@Autowired
	private MenuPermitRepository menuPermitRepository;
	@Autowired
	private ExistException existException;
	@Autowired
	private MenuService menuService;
	@Autowired
	private PermitService permitService;

	@Override
	public String module() {
		return "/admin/menu_permit";
	}

	@Override
	public MenuPermitRepository getRepository() {
		return menuPermitRepository;
	}

	@Override
	public Class<MenuPermit> getEntityClazz() {
		return MenuPermit.class;
	}

	@Override
	public MenuPermit add(MenuPermitForm form) {
		if (form.getMenuId() != null && form.getMenuId() != 0) {
			SysMenu menu = menuService.findById(form.getMenuId());
			form.setMenu(menu);
		}
		if (form.getPermitId() != null && form.getPermitId() != 0) {
			Permit permit = permitService.findById(form.getPermitId());
			form.setPermit(permit);
		}
		form.setCode(form.getMenu().getCode() + ":"
				+ form.getPermit().getCode());
		return super.add(form);
	}

	@Override
	public Map<String, SearchFilter> buildByForm(MenuPermitForm form) {
		Map<String, Object> searchParams = new HashMap<String, Object>();
		Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
		if (form.getMenuId() != null && form.getMenuId() != 0) {
			filters.put("menu.id", new SearchFilter("menu.id", Operator.EQ,
					form.getMenuId()));
		}
		if (form.getPermitId() != null && form.getPermitId() != 0) {
			filters.put("permit.id", new SearchFilter("permit.id", Operator.EQ,
					form.getPermitId()));
		}
		return filters;
	}

	@Override
	public List<MenuPermit> findsByMenuId(Long menuId) {
		return menuPermitRepository.findsByMenuId(menuId);
	}

	@Override
	public SysMenu findPreFix(Long preFixId) {
		return menuService.findById(preFixId);
	}

	@Override
	public Permit findPostFix(Long postFixId) {
		return permitService.findById(postFixId);
	}

	// @CacheEvict(value = "myMenuCache", key = "#form.getId()")
	@Transactional
	@Override
	public void removeRelated(MenuPermitForm form) {
		if (form.getId() != null) {
			menuPermitRepository.delete(form.getId());
		} else {
			List<MenuPermit> userRoles = findByForm(form);
			menuPermitRepository.delete(userRoles);
		}
	}

	@Transactional
	@Override
	public MenuPermit setRelated(MenuPermitForm form) {
		MenuPermit menuPermit = super.setRelated(form);
		menuPermit.setCode(menuPermit.getMenu().getCode() + ":"
				+ menuPermit.getPermit().getCode());
		super.edit(menuPermit);
		return menuPermit;
	}

	@Override
	public List<MenuPermit> findRelateds(MenuPermit entity) {
		MenuPermitForm form = new MenuPermitForm();
		if (entity.getPreFixId() != null && entity.getPreFixId() != 0) {
			form.setPreFixId(entity.getPreFixId());
		}
		if (entity.getPostFixId() != null && entity.getPostFixId() != 0) {
			form.setPostFixId(entity.getPostFixId());
		}
		return findByForm(form);
	}

	@Override
	public List<SysMenu> findUnPreFix(Long permitId) {
		return menuService.findUnsByPermitId(permitId);
	}

	@Override
	public List<Permit> findUnPostFix(Long menuId) {
		return permitService.findUnsByMenuId(menuId);
	}
}
